package cn.xiu.drugstore.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.xiu.drugstore.dao.CustomerDao;
import cn.xiu.drugstore.pojo.Customer;
import cn.xiu.drugstore.util.BaseDao;

/**
 * 顾客实现类
 * @author 陈亚鹏
 *
 */
public class CustomerDaoImpl extends BaseDao implements CustomerDao {
	/**
	 * 查看会员信息
	 */
	@Override
	public List<Customer> queryAllCustomer() throws Exception {
		List<Customer> list=new ArrayList<Customer>();
		String sql="SELECT cust_id,cust_name,cust_sex,cust_age,cust_phone,cust_address,cust_money,cust_level,cust_vp FROM customer ORDER BY cust_id DESC ";
		ResultSet rs=super.executeQuery(sql, null);
		while(rs.next()){
			Customer cust=new Customer();
			cust.setCustId(rs.getInt("cust_id"));
			cust.setCustName(rs.getString("cust_name"));
			cust.setCustSex(rs.getString("cust_sex"));
			cust.setCustAge(rs.getInt("cust_age"));
			cust.setCustPhone(rs.getString("cust_phone"));
			cust.setCustAdress(rs.getString("cust_address"));
			cust.setCustMoney(rs.getDouble("cust_money"));
			cust.setCustIntegral(rs.getDouble("cust_level"));
			cust.setCustClass(rs.getInt("cust_vp"));
			list.add(cust);
		}
		super.closeResource(rs);
		return list;
	}
	/**
	 * 通过会员ID查找会员信息
	 */

	@Override
	public Customer queryCustomerById(int custid) throws Exception {
		Customer news = null;
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT cust_id,cust_name,");
		sql.append("cust_sex,cust_age,cust_phone,");
		sql.append("cust_address,cust_money,cust_level,");
		sql.append("cust_address,cust_money,cust_level,cust_vp");
		sql.append(" FROM customer WHERE cust_id=?");
		Object[] params = { custid };
		ResultSet rs = super.executeQuery(sql.toString(), params);
		if (rs != null && rs.next()) {
			news = new Customer();
			news.setCustId(rs.getInt("cust_id"));
			news.setCustName(rs.getString("cust_name"));
			news.setCustSex(rs.getString("cust_sex"));
			news.setCustAge(rs.getInt("cust_age"));
			news.setCustPhone(rs.getString("cust_phone"));
			news.setCustAdress(rs.getString("cust_address"));
			news.setCustMoney(rs.getDouble("cust_money"));
			news.setCustIntegral(rs.getDouble("cust_level"));
			news.setCustClass(rs.getInt("cust_vp"));
		}
		return news;
	}
	/**
	 * 增加会员
	 */
	@Override
	public int add(Customer cust) throws Exception {
		String sql = "INSERT INTO customer (cust_id,cust_name,cust_sex,cust_age,cust_phone,cust_address,cust_money,cust_level,cust_vp) VALUES(?,?,?,?,?,?,?,?,?)";
		Object[] params = { cust.getCustId(), cust.getCustName(), cust.getCustSex(),
				cust.getCustAge(), cust.getCustPhone(), cust.getCustAdress(),
				cust.getCustMoney(),cust.getCustIntegral(),cust.getCustClass()};
		int result = super.executeUpdate(sql, params);
		return result;
	}
	/**
	 * 删除会员
	 */
	@Override
	public int deleteCustomer(int id) throws Exception {
		String sql="DELETE FROM  customer WHERE cust_id=?";
		Object[] params={id};
		return super.executeUpdate(sql, params);
	}
}
